package com.fg.cloud.factory.controller.factory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.ArrayUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.WarehouseEntity;
import com.fg.cloud.manager.service.factory.WarehouseService;

/**
 * 
 * @author liushengchao
 * @ClassName WarehouseController
 * @Description: 仓库处理Controller 
 * @date 2017年6月13日上午11:23:07
 *
 */
@Controller
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController{
	
	@Autowired
	private WarehouseService wareHouseService;
	
	/**
	 * 厂商仓库页
	 */
	@RequestMapping("/index")
	public String facinfo(HttpServletRequest request, HttpServletResponse response, Model model) {
		System.out.println("===厂商仓库========");
		return "/factory/warehouse";
	}
	
	/**
	 * 
	 * @Description: 添加仓库信息 
	 * @param request
	 * @param response
	 * @param wareHouseEntity  仓库信息实体
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月13日下午2:43:10
	 * @throws 
	 *
	 */
	@RequestMapping(value = "/insert", method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void addWareHouse(HttpServletRequest request, HttpServletResponse response,WarehouseEntity wareHouseEntity){
		try {
			wareHouseEntity.setFacId(getCurrentFactoryUser().getFacId());
			wareHouseEntity.setCreator(getCurrentFactoryUser().getId());
			
			if (wareHouseEntity == null || StringUtils.isEmpty(wareHouseEntity.getWhName())
					|| wareHouseEntity.getFacId() == null || StringUtils.isEmpty(wareHouseEntity.getWhNumber())
					|| wareHouseEntity.getWhManagerName() == null || wareHouseEntity.getManagerPhone() ==null
					|| wareHouseEntity.getWhAddress() == null) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return;
			}
			
			// 名称校验
			int num = wareHouseService.findnum(wareHouseEntity);
			if(num > 0){
				doResult(response, false, StatusCode.SAVEERROR, null, "仓库名称已存在");
				return;
			}
			//插入默认仓库状态
			if (null == wareHouseEntity.getWhStatus()) wareHouseEntity.setWhStatus(1);
			
			int temp = wareHouseService.insert(wareHouseEntity);
			if (temp > 0) {
				doResult(response, true, StatusCode.SAVESUCCESS, temp, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, temp, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			doResult(response, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Description: 分页查询结果 
	 * @param req
	 * @param resp 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月13日下午2:57:24
	 * @throws 
	 *
	 */
	@RequestMapping(value="/selectpage",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void selectPage(HttpServletRequest req , HttpServletResponse resp , String whName,
			Long facId ,int page,int rows){
		try{
			facId = getCurrentFactoryUser().getFacId();
			PageUtils<WarehouseEntity> pageinfo = new PageUtils<WarehouseEntity>(page, rows);
			Map<String,Object> param = new HashMap<String, Object>();
			if( whName !=null && !"".equals(whName) ){
				param.put("whName", whName);
			}
			if(facId != null && facId >0){
				param.put("facId", facId);
			}
			pageinfo.setCondition(param);
			List<WarehouseEntity> list = wareHouseService.selectforPage(pageinfo);
			pageinfo.setData(list);
			if (list.size() == 0) {
				doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_NULL);
			} else {
				doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
			}
		}catch(Exception e){
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Description: 单个查找仓库信息 
	 * @param req
	 * @param resp
	 * @param id 仓库id
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月13日下午3:11:54
	 * @throws 
	 *
	 */
	@RequestMapping(value="find",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void find(HttpServletRequest req , HttpServletResponse resp, Long id){
		try{
			if (id == null) {
				doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.INPUT_NULL);
				return;
			}
			WarehouseEntity warehouse = wareHouseService.find(id);
			
			if (warehouse == null) {
				doResult(resp, true, StatusCode.FINDSUCCESS, warehouse, ResultType.FIND_NULL);
			} else {
				doResult(resp, true, StatusCode.FINDSUCCESS, warehouse, ResultType.FIND_SUCCESS);
			}
		}catch(Exception e){
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Description: 更新仓库基础信息 
	 * @param req
	 * @param resp
	 * @param warehouseEntity 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月13日下午3:37:22
	 * @throws 
	 *
	 */
	@RequestMapping(value="/modify",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void update(HttpServletRequest req , HttpServletResponse resp , WarehouseEntity warehouseEntity){
		try{
			warehouseEntity.setUpdater(getCurrentFactoryUser().getId());
			warehouseEntity.setFacId(getCurrentFactoryUser().getFacId());
			if(warehouseEntity == null || warehouseEntity.getId() == null || warehouseEntity.getId() < 1 ){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
			}else{

				// 名称校验
				int num = wareHouseService.findnum(warehouseEntity);
				if(num > 0){
					doResult(resp, false, StatusCode.SAVEERROR, null, "仓库名称已存在");
					return;
				}
				warehouseEntity.setFacId(getCurrentFactoryUser().getFacId());
				int flag = wareHouseService.update(warehouseEntity);
				if(flag > 0 ){
					doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
				}else{
					doResult(resp, false, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
				}
			}
		}catch(Exception e){
			doResult(resp, true, StatusCode.SAVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * @Description: 删除单个仓库信息。 (废弃暂留)
	 * @param req
	 * @param resp
	 * @param id 仓库信息ID
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月13日下午3:40:46
	 * @throws 
	 *
	 */
	//@RequestMapping(value="/delete",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void delete(HttpServletRequest req , HttpServletResponse resp , @Param("id") Long id){
		try {
			int flag = wareHouseService.delete(id);
			if(flag > 0 ){
				doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			}else{
				doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:批量删除
	 * @param req
	 * @param resp
	 * @param ids
	 * @author 陈银龙
	 * @date 2017年8月8日上午11:10:58
	 */
	@RequestMapping(value="/deletes",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void deletes(HttpServletRequest req , HttpServletResponse resp , String ids){
		try {
			Long[] id;
			if(ids ==null || "".equals(ids)){
				id = new Long[0];
			}else{
				String[] id1 = ids.split(",");
				id = ArrayUtils.convert(id1);
			}

			Long faceId = getCurrentFactoryUser().getFacId();
			Map<String,Object> condition = new HashMap<String,Object>();
			condition.put("ids", id);
			condition.put("facId", faceId);
			int flag = 0;
			if(id.length>0){
				flag = wareHouseService.deletes(condition);
			}else{
				flag = 1;
			}
			if(flag > 0 ){
				doResult(resp, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			}else{
				doResult(resp, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			doResult(resp, true, StatusCode.REMOVEERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Description: 仓库列表,不分页 
	 * @param req
	 * @param resp 
	 * @return void
	 * @author liushengchao
	 * @date 2017年6月13日下午2:57:24
	 * @throws 
	 *
	 */
	@RequestMapping(value="/selectWarehouse",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public void selectWarehouse(HttpServletRequest req , HttpServletResponse resp){
		try{
			List<WarehouseEntity> list = wareHouseService.selectWarehouse(getCurrentFactoryUser().getFacId());
			if (list.size() == 0) {
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_NULL);
			} else {
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}
		}catch(Exception e){
			doResult(resp, true, StatusCode.FINDERROR, null, ResultType.ERROE);
			e.printStackTrace();
		}
	}
}
